package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Group;
import com.gxa.zhonglong.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 14:42
 * Description:分组操作接口
 * Version: V1.0
 */
@Api(tags ="分组接口api")
@CrossOrigin
@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @PostMapping("/selectAllById")
    @ApiOperation("根据分店查询所有分组信息")
    public Map<String,Object> selectAllById(Long id) throws BusinessException {
        final List<Map<String, Object>> maps = groupService.selectAllById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改分组")
    public Map<String,Object> addOrUpdate(Group group) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (group.getId()==null){
            groupService.add(group);
            jsonResult.setMsg("添加成功!");
        }else{
            groupService.update(group);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("根据id删除分组")
    public Map<String,Object> del(Long id) throws BusinessException {
        groupService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
